package com.shoulder.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shoulder.boot.acl.mapper.RoleMapper;
import com.shoulder.boot.system.dto.TenantAddDTO;
import com.shoulder.boot.system.dto.TenantModDTO;
import com.shoulder.boot.system.dto.TenantQueryDTO;
import com.shoulder.boot.system.entity.Tenant;
import com.shoulder.boot.system.exception.SystemException;
import com.shoulder.boot.system.mapper.TenantMapper;
import com.shoulder.boot.system.service.TenantService;
import com.shoulder.boot.system.utils.TenantUtils;
import com.shoulder.boot.system.vo.TenantVo;
import com.shoulder.core.fetch.BaseIds;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static com.shoulder.boot.system.entity.Tenant.DISABLE_STATUS;
import static com.shoulder.boot.system.entity.Tenant.NOT_DISABLE_STATUS;

/**
 * 租户信息 服务实现类
 *
 * @author 59676880@qq.com
 * @since 2024-03-14 14:36:09
 */
@Service
@Transactional
@AllArgsConstructor
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements TenantService {

    private final RoleMapper roleMapper;

    @Override
    public List<TenantVo> listQuery(TenantQueryDTO queryDTO) {
        List<Tenant> tenants = this.baseMapper.selectList(TenantUtils.buildQueryWrapper(queryDTO));
        if (CollectionUtils.isEmpty(tenants)) {
            return Collections.emptyList();
        }
        return TenantUtils.buildUtils(tenants, roleMapper).toVos(tenants);
    }

    @Override
    public TenantVo look(String id) {
        Tenant entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("数据不存在");
        }
        return TenantUtils.buildUtils(Collections.singletonList(entity), roleMapper).toVo(entity);
    }

    @Override
    public List<TenantVo> looks(BaseIds<String> baseIds) {
        List<Tenant> tenants = this.baseMapper.selectBatchIds(baseIds.getIds());
        if (CollectionUtils.isEmpty(tenants)) {
            return Collections.emptyList();
        }
        return TenantUtils.buildUtils(tenants, roleMapper).toVos(tenants);
    }

    @Override
    public void del(String id) {
        Tenant entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("数据不存在");
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void batchDel(BaseIds<String> baseIds) {
        this.baseMapper.deleteBatchIds(baseIds.getIds());
    }


    @Override
    public Page<TenantVo> pageQuery(TenantQueryDTO queryDTO) {
        Page<Tenant> selectPage = this.baseMapper.selectPage(new Page<>(queryDTO.getCurrent(), queryDTO.getSize())
                , TenantUtils.buildQueryWrapper(queryDTO));
        if (!ObjectUtils.isEmpty(selectPage) && !CollectionUtils.isEmpty(selectPage.getRecords())) {
            TenantUtils tw = TenantUtils.buildUtils(selectPage.getRecords(), roleMapper);
            return tw.toPage(selectPage);
        }
        return new Page<>();
    }

    @Override
    public void mod(TenantModDTO modDTO) {
        Tenant info = Optional.ofNullable(this.baseMapper.selectById(modDTO.getId())).orElseThrow(() -> new SystemException("租户信息不存在"));
        Tenant tenant = this.baseMapper.selectOne(new LambdaQueryWrapper<Tenant>()
                .eq(Tenant::getAccount, modDTO.getAccount()).ne(Tenant::getId, modDTO.getId()));
        if (!ObjectUtils.isEmpty(tenant)) {
            throw new SystemException("租户账号已存在");
        }
        BeanUtils.copyProperties(modDTO, info);
        this.baseMapper.updateById(info);
    }

    @Override
    public void add(TenantAddDTO addDTO) {
        Tenant info = this.baseMapper.selectOne(new LambdaQueryWrapper<Tenant>().eq(Tenant::getAccount, addDTO.getAccount()));
        if (!ObjectUtils.isEmpty(info)) {
            throw new SystemException("租户账号已存在");
        }

        info = new Tenant();
        BeanUtils.copyProperties(addDTO, info);
        this.baseMapper.insert(info);
    }

    @Override
    public void switchStatus(String id) {
        // 租户存在则改变状态
        Tenant entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("租户不存在");
        }
        entity.setStatus(DISABLE_STATUS.equals(entity.getStatus()) ? NOT_DISABLE_STATUS : DISABLE_STATUS);
        this.baseMapper.updateById(entity);
    }


}
